# -*- coding: utf-8 -*-
import os

from google.appengine.ext import webapp

from google.appengine.ext import db
from google.appengine.api import mail

from google.appengine.ext.webapp import template

from pagesConfiguration import pages

class Feedback(db.Expando):
    name = db.StringProperty()
    email = db.EmailProperty()
    message = db.TextProperty()
    company = db.StringProperty()
    ip = db.StringProperty()
    date = db.DateTimeProperty(auto_now_add=True)

class PageFeedBack(webapp.RequestHandler):
    def get(self):
        p = pages.Pages()
      
        path = os.path.join(os.getcwd(), 'html/feedback.html')
          
        self.response.out.write(p.getHeader(':: Feedback '))
        self.response.out.write(template.render(path, {}))
        self.response.out.write(p.getFoot(self.request.remote_addr))

class RegisterFeedBack(webapp.RequestHandler):
    def post(self):
        feed = Feedback()
        
        feed.name = self.request.get("name")
        feed.email = self.request.get("email")
        feed.message = self.request.get("message")
        feed.company = self.request.get("company")
        feed.ip = self.request.remote_addr
        feed.put()
        
        #try:
        mail.send_mail(sender='WAKAME Metamodeling <wakame@brenomachado.org>',
              to = feed.email,
              bcc = 'brenomachado@gmail.com',
              subject = 'WAKAME Metamodeling Feedback - http://wakame.brenomachado.org',
              body = 'Message from: %s (%s) - %s \n\n\n %s \n\n\nDate: %s - IP: %s' % \
                     (feed.name, feed.email, feed.company, feed.message, feed.date, feed.ip))
                     
        template_values = {'msg': 'Feedback sent with SUCESS!'}
    
        path = os.path.join(os.getcwd(), 'html/message.html')
        
        p = pages.Pages()
        
        self.response.out.write(p.getHeader(':: SUCCESS! '))
        self.response.out.write(template.render(path, template_values))
        self.response.out.write(p.getFoot(self.request.remote_addr))
        return
        #except:
        #    template_values = {'msg': 'ERROR on sending the email Feedback!'}
            
        #    path = os.path.join(os.getcwd(), 'html/message.html')
            
        #    p = pages.Pages()
            
        #    self.response.out.write(p.getHeader(':: ERROR! '))
        #    self.response.out.write(template.render(path, template_values))
        #    self.response.out.write(p.getFoot(self.request.remote_addr))
        
        #    return
